from flask import jsonify, request

from db.dao import NewsDao
from db.models import News
from utils.util import FlaskTool
from web import custom
from service.text_analysis.NewsService import analysis


@custom.route('/news/list')
def newsInfo():
    '''
    股票市场单日涨幅查询
    :return:
    '''
    pagination = NewsDao().queryByPage(request.args)
    return jsonify(FlaskTool.paginatioToList(pagination))


@custom.route('/news/extract_keys')
def extract_keys():
    '''
    股票市场单日涨幅查询
    :return:
    '''
    analysis(request.args.get('id'))
    return 'ok'


@custom.route('/news/update', methods=['POST'])
def update_news():
    '''
    股票市场单日涨幅查询
    :return:
    '''

    newsInfo = News()
    setattr(newsInfo, 'id', request.form['id'])
    setattr(newsInfo, 'concept', request.form['concept'])
    setattr(newsInfo, 'industry', request.form['industry'])
    setattr(newsInfo, 'keys', request.form['keys'])
    setattr(newsInfo, 'stock_code', request.form['stock_code'])
    NewsDao().updateById(newsInfo)
    return 'ok'


@custom.route('/news/remove')
def remove_news():
    '''
    股票市场单日涨幅查询
    :return:
    '''

    NewsDao().removeBatch(request.args.get('ids'))
    return 'ok'
